package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.GoodsListAdapter;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.internal.component.DaggerSubClassifyComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.GoodsListModule;
import com.zw.yzk.kabu.model.GoodsModel;
import com.zw.yzk.kabu.presenter.SearchClassifyPresenter;
import com.zw.yzk.kabu.view.view.SubClassifyView;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by zhanwei on 2017/3/19.
 */

public class SearchClassifyActivity extends BaseActivity implements SubClassifyView {

    @Bind(R.id.goods)
    RecyclerView goods;
    @Bind(R.id.empty)
    View empty;

    @Inject
    SearchClassifyPresenter presenter;
    private GoodsListAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_search_classify;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
    }

    @Override
    public void initComponent() {
        DaggerSubClassifyComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .goodsListModule(new GoodsListModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initRecyclerView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void getGoodsSuccess(List<GoodsModel> list) {
        if(adapter == null) {
            adapter = new GoodsListAdapter(list);
            goods.setAdapter(adapter);
        } else {
            adapter.refresh(list);
        }

        refreshEmptyStatus(adapter.getItemCount() == 0);
    }

    @Override
    public void getGoodsFailed() {
    }

    @OnClick(R.id.back)
    public void back() {
        finish();
    }

    @OnTextChanged(value = R.id.search, callback = OnTextChanged.Callback.TEXT_CHANGED)
    void onTextChanged(CharSequence s, int start, int before, int count) {
        getGoods(s.toString());
    }

    @OnClick(R.id.right_image)
    public void startShoppingCart() {
        startActivity(new Intent(this, ShoppingCartActivity.class));
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        goods.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false));
    }

    /**
     * 是否显示空提示
     *
     * @param show true:显示，false:不显示
     */
    private void refreshEmptyStatus(boolean show) {
        empty.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    /**
     * 获取商品列表
     */
    private void getGoods(String key) {
        presenter.getGoods(getIntent().getIntExtra(ActivityConstant.GOODS_CLASSIFY_TYPE, 0), key);
    }
}
